from conf.settings import CASE_DATA_PATH
from common.utils import get_mysql
import os
import xlrd

class Param:

    @classmethod
    def from_csv(cls,name):
        '''从csv文件/txt文件读取参数化内容'''
        abs_path = os.path.join(CASE_DATA_PATH,name)
        data = []
        with open(abs_path,encoding='utf-8') as fr:
            for line in fr:
                line = line.strip() #去除line头尾空格和空行
                if line:
                    data.append(line.split(','))#将每行line的数据以','分割,加到data,形成一个二维数组

        return data

    @classmethod
    def from_excel(cls,name):
        '''从xls文件读取参数化内容'''
        abs_path = os.path.join(CASE_DATA_PATH,name)
        data = []

        book = xlrd.open_workbook(abs_path)
        sheet = book.sheet_by_index(0)
        for i in range(1,sheet.nrows):
            print(sheet.row_values(i))
            data.append(sheet.row_values(i))

        return data

    @classmethod
    def from_mysql(cls,sql,db = None,node = 'mysql'):
        m = get_mysql(node,db=db,cursor_type=None)
        return m.fetchall(sql)

    @classmethod
    def from_redis(cls):
        pass

    @classmethod
    def from_mongdb(cls):
        pass

if __name__ == "__main__":
    print(Param.from_excel('users.xlsx'))